package com.example.job.entity;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.job.db.userdb;

public class UserInfo {
    private int user_id;
    private String username;
    private String password;
    private String job_id;
    private static final String PREFS_NAME = "user";
    private static final String KEY_USERNAME = "username";
    private static UserInfo instance;

    public UserInfo(int user_id, String username, String password) {
        this.user_id = user_id;
        this.username = username;
        this.password = password;
    }

    public UserInfo(int user_id, String username, String password, String job_id) {
        this.user_id = user_id;
        this.username = username;
        this.password = password;
        this.job_id = job_id; // 初始化 job_id
    }

    private UserInfo(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        this.username = sharedPreferences.getString(KEY_USERNAME, "默认用户名");
    }

    public static UserInfo getUserInfo(Context context) {
        if (instance == null) {
            userdb dbHelper = userdb.getInstance(context);
            String username = getUsername(context);
            UserInfo userInfo = dbHelper.getUserInfoByUsername(username);
            if (userInfo != null) {
                instance = userInfo;
            } else {
                instance = new UserInfo(0, "默认用户名", ""); // 默认值
            }
        }
        return instance;
    }

    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getJob_id() {
        return job_id;
    }

    public void setJob_id(String job_id) {
        this.job_id = job_id;
    }


    private static String getUsername(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(KEY_USERNAME, "默认用户名");
    }

    public static UserInfo getFreshUserInfo(Context context) {
        userdb dbHelper = userdb.getInstance(context);
        String username = getUsername(context);
        return dbHelper.getUserInfoByUsername(username);
    }
}
